<?php

class Packaging extends Database
{
    protected $id;
	protected $product_id;
	protected $countpack;
	protected $count;
	
	function Packaging($id,$product_id,$countpack,$count)
	{
        parent::__construct();
        $this->id=$id;
        $this->product_id=$product_id;
		$this->countpack=$countpack;
		$this->count=$count;
	}

    function add()
    {
        mysql_query("INSERT INTO packaging (`product_id`,countpack,count) VALUES ('$this->product_id','$this->countpack','$this->count')");
    }

    function plusAmount()
    {
        $sql=mysql_query("SELECT amount FROM products WHERE id='$this->product_id'");
        $sql=mysql_fetch_array($sql);
        $sql[0]=$sql[0]+$this->count*$this->countpack;
        mysql_query("UPDATE products SET `amount`='$sql[0]' WHERE id='$this->product_id'");
    }

    function plus()
    {
        $sql=mysql_query("SELECT count FROM packaging WHERE product_id='$this->product_id' AND countpack='$this->countpack'");
        if(mysql_num_rows($sql)==0)
        {
            mysql_query("INSERT INTO packaging (`product_id`,countpack,count) VALUES ('$this->product_id','$this->countpack','$this->count')");
            $this->plusAmount();
        }
        else
        {
            $sql=mysql_fetch_array($sql);
            $this->count=$this->count+$sql[0];
            mysql_query("UPDATE packaging SET `count`='$this->count' WHERE product_id='$this->product_id' AND countpack='$this->countpack'");
            $this->plusAmount();
        }

    }

    function show()
    {
        $sql=mysql_query("SELECT countpack,count FROM packaging WHERE product_id='$this->product_id' ORDER BY countpack DESC ");
        print("
		<p align=center>
		<table align=center border=1 cellpadding=5 cellspacing=0>
		 <tr bgcolor=#EEEEEE>
		  <td width=100><b>Кількість в упаковці</b></td>
		  <td width=100><b>Кількість упаковок</b></td>
		 </tr>
		");
        while ($row = mysql_fetch_array($sql))
        {
            print("
			<tr>
			");
            print("
			 <td>
			 	".$row[0]."
			 </td>
			 <td>
			 	".$row[1]."
			 </td>
			 ");
            print("</tr>");
        }
        print("</table>");
        print("<p align=center><br>");
    }

    function showFree()
    {
        $sql=mysql_query("SELECT countpack,count FROM packaging WHERE product_id='$this->product_id' ORDER BY countpack DESC");
        print("
		<table align=center border=1 cellpadding=5 cellspacing=0>
		 <tr bgcolor=#EEEEEE>
		  <td width=100><b>Кількість в упаковці</b></td>
		  <td width=100><b>Кількість упаковок</b></td>
		  <td width=100><b></b></td>
		 </tr>
		");
        while ($row = mysql_fetch_array($sql))
        {
            print("
			<tr>
			");
            print("
			 <td>
			 	".$row[0]."
			 </td>
			 <td>
			 	".$row[1]."
			 </td>
			 <td>
                <input type=text name=countpack".$row[0]." size=10 value='"); if(isset($_POST['submitfree'])) print($_POST['countpack'.$row[0]]); print("'/>
			 </td>
			 ");
            print("</tr>");
        }
        print("</table>");
        print("<p align=center><br>");
    }

}

?>